package com.carey.blog.action.impl;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.carey.blog.action.util.Filler;
import com.carey.blog.framework.latke.action.ActionException;
import com.carey.blog.framework.latke.model.Role;
import com.carey.blog.framework.latke.model.User;
import com.carey.blog.framework.latke.service.LangPropsService;
import com.carey.blog.framework.latke.util.Locales;
import com.carey.blog.model.Common;
import com.carey.blog.model.Preference;
import com.carey.blog.util.Preferences;
import com.carey.blog.util.Users;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/**
 * Admin index action. admin-index.ftl.
 * 
 */
public final class AdminIndexAction extends AbstractAdminAction {

	/**
	 * Default serial version uid.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(AdminIndexAction.class.getName());
	/**
	 * Language service.
	 */
	private LangPropsService langPropsService = LangPropsService.getInstance();
	/**
	 * Filler.
	 */
	private Filler filler = Filler.getInstance();
	/**
	 * Preference utilities.
	 */
	private Preferences preferenceUtils = Preferences.getInstance();
	/**
	 * User utilities.
	 */
	private Users userUtils = Users.getInstance();
	/**
	 * User service.
	 */
	private UserService userService = UserServiceFactory.getUserService();

	@Override
	protected Map<?, ?> doFreeMarkerAction(
			final freemarker.template.Template template,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		final Map<String, Object> ret = new HashMap<String, Object>();

		try {
			final JSONObject preference = preferenceUtils.getPreference();
			if (null == preference) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
				return ret;
			}

			final String localeString = preference
					.getString(Preference.LOCALE_STRING);
			final Locale locale = new Locale(Locales.getLanguage(localeString),
					Locales.getCountry(localeString));

			final Map<String, String> langs = langPropsService.getAll(locale);
			ret.putAll(langs);

			final JSONObject currentUser = userUtils.getCurrentUser();
			String userName = null;
			String roleName = null;
			if (null == currentUser) {
				// The administrators may be added via GAE Admin Console
				// Permissions
				final com.google.appengine.api.users.User collaborateAdmin = userService
						.getCurrentUser();
				userName = collaborateAdmin.getNickname();
				roleName = Role.ADMIN_ROLE;
			} else {
				userName = currentUser.getString(User.USER_NAME);
				roleName = currentUser.getString(User.USER_ROLE);
			}

			ret.put(User.USER_NAME, userName);
			ret.put(User.USER_ROLE, roleName);

			filler.fillBlogHeader(ret, preference);
			filler.fillBlogFooter(ret, preference);

			final boolean hasMultipleUsers = userUtils.hasMultipleUsers();
			ret.put(Common.ENABLED_MULTIPLE_USER_SUPPORT, hasMultipleUsers);
		} catch (final Exception e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new ActionException(e);
		}

		return ret;
	}

	@Override
	protected JSONObject doAjaxAction(final JSONObject data,
			final HttpServletRequest request, final HttpServletResponse response)
			throws ActionException {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
